<?php
use think\Db;
//生成订单号
//$pre='',$pre='pay',$pre='rech'
function create_order_sn($pre=''){
    $strlen=strlen($pre);
    while(true) {
        $order_sn = date('YmdHis') . mt_rand(1000, 9999);
        if ($strlen < 8) {
            $len = 8 - $strlen;
            $order_sn = date('YmdHis') . mt_rand(pow(10, $len - 1), pow(10, $len) - 1);
        }
        $model = new \app\index\model\OrderModel();
        if(!$model->hasExistedOrder($order_sn,$pre)){
            break;
        }
    }
    return array(
        'order_sn'=>$order_sn,
        'type'=>$pre,
        'out_trade_no'=>$pre.$order_sn
    );
}
//获取订单号
function get_order_sn($out_trade_no){
    $pre=getOrderType($out_trade_no);
    $order_sn=$out_trade_no;
    $strlen=strlen($pre);
    if($strlen>0){
        $order_sn= ltrim($out_trade_no,$pre);
    }
    return array(
        'order_sn'=>$order_sn,
        'type'=>$pre,
        'out_trade_no'=>$out_trade_no
    );
}

function getOrderType($out_trade_no){
    preg_match('/^[a-zA-Z]{1,}/u' , $out_trade_no, $result);
    if(!empty($result)){
        return $result[0];
    }
    return '';
}

//更新订单状态
function update_pay_status($pay=array()){
    $sn=get_order_sn($pay['out_trade_no']);
    $order=Db::name('order')->field('money AS order_amount,order_id,user_id,car_id,type')->where(array('order_sn'=>$sn['order_sn'],'pay_status'=>0))->find();
    if(!isset($order['order_amount']) || $order['order_amount']!=$pay['price'])
        exit("fail"); //验证失败
    $map=array(
        'order_id'=>$order['order_id'],
        'pay_status'=>1,
        'pay_time'=>time()
    );
    if(isset($pay['transaction_id'])){
        $map['transaction_id']=$pay['transaction_id'];
    }
    switch ($order['type']){
        case 2://刷新
            Db::name('order')->where(array('order_id'=>$order['order_id']))->update($map);
            $conf=json_decode(Db::name('config')->where(array('inc_type'=>'settopInfo'))->value('value'),true);
            Db::name('car')->where(array('car_id'=>$order['car_id']))->update(array('settop_time'=>time()+$conf['expiryHour']*60*60,'update_time'=>time()));
            break;
        case 3://置顶
            Db::name('order')->where(array('order_id'=>$order['order_id']))->update($map);
            $conf=json_decode(Db::name('config')->where(array('inc_type'=>'refreshInfo'))->value('value'),true);
            Db::name('car')->where(array('car_id'=>$order['car_id']))->update(array('refresh_time'=>time()+$conf['expiryHour']*60*60,'update_time'=>time()));
            break;
        default://充值
            Db::name('order')->where(array('order_id'=>$order['order_id']))->update($map);
            $user_money=Db::name('user')->where(array('user_id'=>$order['user_id']))->value('user_money')+$pay['price'];
            Db::name('user')->where(array('user_id'=>$order['user_id']))->update(array('user_money'=>$user_money));
    }
    return $order;
}

//认证短信验证码快捷函数
function checkMobileVerifyCode($param){
    if(!isset($param['mobile'])) return false;
    $smsLogModel=new \app\index\model\SmsLogModel();
    $smsLog=$smsLogModel->findSmsLog($param);
    if(!empty($smsLog)){
        if($param['code'] == $smsLog['code']){
            return true;
        }
    }
    return false;
}

function explodeImage(&$param){
    $param=explode(",", $param);
    foreach($param as &$v){
        $v=imageUrl($v);
    }
}

function head_pic($img){
    if(is_null($img) || $img=='') return config('view_replace_str.__IMG__').'/head_pic.png';
    $domain=request()->domain();
    if(strpos($img,'/upload/')===0) $img=$domain.$img;
    return $img;
}

function imageUrl($img){
    if(is_null($img)) return '';
    $domain=request()->domain();
    if(strpos($img,'/upload/')===0) $img=$domain.$img;
    return $img;
}